<?php
namespace app\index\service;

use app\index\controller\Opt;
use app\index\model\CityModel;
use app\index\model\DeptModel;
use app\index\model\MainOptionModel;
use app\index\model\OptionModel;
use app\index\model\UserGangModel;

class ImportService extends CommonService
{

    /**
     *   用户    检查导入的是否重复
     * @param $data
     * @param $check_field  string  要检查的字段
     * @param $num    int   当前行
     * @param $check_value  string  当前字段的值
     * @return string
     */
    public function check_import_repeat($data, $check_field, $num, $check_value){
        $str = '';
        foreach($data as $k=>$v){
            if($k == $num) continue;
            $hang = "行".($k + 1);
            if($v[$check_field] == $check_value){
                $str = $hang;
            }
        }
        return $str;
    }


    /**
     * 用户    检查归属部门 是否输入正确
     * @param $value
     * @return bool|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check_import_deptid($value){
        $data = DeptModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$value])->find();
        if(!$data){
            return false;
        }
        return $data['id'];
    }

    /**
     * 用户    检查归属岗位 是否输入正确
     * @param $deptid
     * @param $value
     * @return bool|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check_import_gangid($deptid, $value){
        $data = UserGangModel::where(['comid'=>$this->userinfo['comid'],'deptid'=>$deptid,'deleted_user'=>0,'name'=>$value])->find();
        if(!$data){
            return false;
        }
        return $data['id'];
    }


    /**
     * 检查省市区
     * @param $values
     * @param int $dig
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check_areas($values, $dig=1){
        $arr = explode('/', $values);
        if(count($arr) != $dig){
            return false;
        }

        $res_pro = CityModel::where(['pid'=>0,'name'=>$arr[0]])->find();
        if(!$res_pro){
            return false;
        }
        if($dig == 1){
            return true;
        }

        $res_city = CityModel::where(['pid'=>$res_pro['id'],'name'=>$arr[1]])->find();
        if(!$res_city){
            return false;
        }
        if($dig == 2){
            return true;
        }

        $res_county = CityModel::where(['pid'=>$res_city['id'],'name'=>$arr[2]])->find();
        if(!$res_county){
            return false;
        }
        return true;
    }


    /**
     * 检查基础数据
     * @param $field
     * @param $value
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check_basedata($field, $value){
        $data = (new OptService())->get_base_info($field);
        $name_str = '';
        $name_arr = [];
        foreach ($data as $k=>$v){
            array_push($name_arr, $v['name']);
            if(empty($name_str)){
                $name_str .= '['.$v['name'].']';
            }else{
                $name_str .= ',['.$v['name'].']';
            }
        }
        if(!in_array($value, $name_arr)){
            return $name_str;
        }
        return true;
    }

    public function get_basedata_keyid($field,$value)
    {
        $topid = OptionModel::where('num',$field)->value('id');
        if(!$topid){
            return '';
        }
        $keyid = OptionModel::where(['topid'=>$topid,'name'=>$value])->value('keyid');
        if($keyid){
            return $keyid;
        }else{
            return '';
        }
    }





    /**
     * 检查造价基础数据
     * @param $field
     * @param $value
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check_main_basedata($field, $value){
        $data = (new MainService())->get_base_info($field);
        $name_str = '';
        $name_arr = [];
        foreach ($data as $k=>$v){
            array_push($name_arr, $v['name']);
            if(empty($name_str)){
                $name_str .= '['.$v['name'].']';
            }else{
                $name_str .= ',['.$v['name'].']';
            }
        }
        if(!in_array($value, $name_arr)){
            return $name_str;
        }
        return true;
    }

    public function get_main_basedata_keyid($field,$value)
    {
        $topid = MainOptionModel::where('num',$field)->value('id');
        if(!$topid){
            return '';
        }
        $keyid = MainOptionModel::where(['topid'=>$topid,'name'=>$value])->value('keyid');
        if($keyid){
            return $keyid;
        }else{
            return '';
        }
    }

}
